﻿using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;

namespace editor
{

    public class strelka_ : Adorner
    {
        // Be sure to call the base class constructor.
        public strelka_(UIElement adornedElement)
            : base(adornedElement)
        {
            VisualBrush _brush = new VisualBrush(adornedElement);

            _child = new Polyline();
            _child.Width = adornedElement.RenderSize.Width;
            _child.Height = adornedElement.RenderSize.Height;
            //_child.
            s = new Point(((Line)adornedElement).X2, ((Line)adornedElement).Y2);
            //DoubleAnimation animation = new DoubleAnimation(0.3, 0.8, new Duration(TimeSpan.FromSeconds(1)));
            //animation.AutoReverse = true;
            //animation.RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever;
            //_brush.BeginAnimation(System.Windows.Media.Brush.OpacityProperty, animation);

            //_child.Fill = _brush;
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout subsystem as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            // Get a rectangle that represents the desired size of the rendered element
            // after the rendering pass.  This will be used to draw at the corners of the 
            // adorned element.
            //Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);
            Polyline adornedElementRect = new Polyline();
            //свойства

            adornedElementRect.StrokeEndLineCap = PenLineCap.Triangle;  //задает наконечник
            adornedElementRect.Stroke = Brushes.Black;                   //способ закраски контура fill не исполь solicolorbrash
            adornedElementRect.StrokeThickness = 2;

            PointCollection myPointCollection2 = new PointCollection();

            myPointCollection2.Add(new Point(s.X - 10, s.Y - 10));
            myPointCollection2.Add(new Point(s.X, s.Y));
            myPointCollection2.Add(new Point(s.X - 10, s.Y + 10));
            adornedElementRect.Points = myPointCollection2;

            // Some arbitrary drawing implements.
            //SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
            //renderBrush.Opacity = 0.2;
            //Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
            //double renderRadius = 5.0;
            ////смещаемм точку влево что бы было по центру
            //Point point_line_down = adornedElementRect.BottomRight;
            //point_line_down.X -= 1;

            Pen df = new Pen(Brushes.Beige, 3);

            //GeometryDrawing 
            LineGeometry liuy1 = new LineGeometry(new Point(s.X - 10, s.Y - 10), new Point(s.X, s.Y));
            LineGeometry liuy2 = new LineGeometry(new Point(s.X - 10, s.Y + 10), new Point(s.X, s.Y));
            //gomet liner=new Geometry();

            // Just draw a circle at each corner.
            //drawingContext.DrawRectangle(renderBrush, renderPen, adornedElementRect);
            //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
            //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
            //drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
            drawingContext.DrawGeometry(Brushes.Black, df, liuy1);
            drawingContext.DrawGeometry(Brushes.Black, df, liuy2);

        }
        Point s;
        private Polyline _child = null;
        private double _leftOffset = 0;
        private double _topOffset = 0;

        protected override Size MeasureOverride(Size constraint)
        {
            _child.Measure(constraint);
            return _child.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            _child.Arrange(new Rect(finalSize));
            return finalSize;
        }

        protected override Visual GetVisualChild(int index)
        {
            return _child;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        public double LeftOffset
        {
            get
            {
                return _leftOffset;
            }
            set
            {
                _leftOffset = value;
                UpdatePosition();
            }
        }

        public double TopOffset
        {
            get
            {
                return _topOffset;
            }
            set
            {
                _topOffset = value;
                UpdatePosition();

            }
        }

        private void UpdatePosition()
        {
            AdornerLayer adornerLayer = this.Parent as AdornerLayer;
            if (adornerLayer != null)
            {
                adornerLayer.Update(AdornedElement);
            }
        }

        public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
        {
            GeneralTransformGroup result = new GeneralTransformGroup();
            result.Children.Add(base.GetDesiredTransform(transform));
            result.Children.Add(new TranslateTransform(_leftOffset, _topOffset));
            return result;
        }
    }
    
}
